<cfsetting enablecfoutputonly="yes">
<cfparam name="token" default="" />
<cfparam name="action" default="" />
<cfparam name="orderID" default="0" />
<cfparam name="orderProductID" default="0" />
<cfparam name="trackingCode" default="" />

<cfif Len(Trim(action)) eq 0>
	<cfoutput>
	<table border="1" cellpadding="8">
		<tr valign="top">
			<th>Action</th>
			<th>Params</th>
			<th>Returns</th>
			<th>Notes</th>
		</tr>
		<tr valign="top">
			<td>getOrder</td>
			<td>
				token guid<br>
				orderID int
			</td>
			<td>Order json</td>
			<td></td>
		</tr>
		<tr valign="top">
			<td>getOrderProducts</td>
			<td>
				token guid<br>
				orderID int
			</td>
			<td>Order Products json</td>
			<td></td>
		</tr>
		<tr valign="top">
			<td>getOrderProduct</td>
			<td>
				token guid<br>
				orderProductID int
			</td>
			<td>Order Product json</td>
			<td></td>
		</tr>
		<tr valign="top">
			<td>saveTracking</td>
			<td>
				token guid,<br>
				orderID int<br>
				orderProductID int *deprecated*<br>
				orderProductIDs csv string *new*<br>
				trackingCode varchar
			</td>
			<td>Boolean</td>
			<td>Leave orderProductID blank to apply tracking code to all items in order</td>
		</tr>
		<tr valign="top">
			<td>deleteTracking</td>
			<td>
				token guid<br>
				trackingCode varchar
			</td>
			<td>Boolean</td>
			<td>Marks tracking code as deleted and removed from each order product</td>
		</tr>
	</table>
	</cfoutput>
<cfelseif token eq "53BCFE6D-C5C0-4E21-B35E-C6EECC727CF9">
	<cfscript>
		orderGateway = new assets.csac.com.gateway.OrderGateway();
		orderProductGateway = new assets.csac.com.gateway.OrderProductGateway();
		trackingGateway = new assets.csac.com.gateway.TrackingGateway();
	</cfscript>
	<cfswitch expression="#action#">
		<cfcase value="getOrder">
			<cfset result = orderGateway.query(Order_ID = orderID) />
		</cfcase>
		<cfcase value="getOrderProducts">
			<cfset result = orderProductGateway.query(Order_ID = orderID) />
		</cfcase>
		<cfcase value="getOrderProduct">
			<cfset result = orderProductGateway.query(Order_Product_ID = orderProductID) />
		</cfcase>
		<cfcase value="saveTracking">
			<cfset qTracking = trackingGateway.query(Tracking_Code = trackingCode) />
			<cfif qTracking.RecordCount eq 0>
				<cfset trackingArgs = {} />
				<cfset trackingArgs.Tracking_ID = 0 />
				<cfset trackingArgs.Tracking_Code = trackingCode />
				<cfset trackingArgs.Order_ID = orderID />
				<cfset trackingArgs.Order_Product_IDs = orderProductIDs />
				<cfset tracking = trackingGateway.save(argumentCollection = trackingArgs) />
				<cfset orderArgs = {} />
				<cfset orderArgs.Order_ID = orderID />
				<cfset orderArgs.Shipped_Date = DateFormat(Now(), "m/d/yyyy") />
				<cfset orderArgs.Status = "Shipped" />
				<cfset order = orderGateway.save(argumentCollection = orderArgs) />
				<cfset result = true />
			<cfelseif qTracking.RecordCount eq 1>
				<cfset tracking = trackingGateway.get(Tracking_ID = qTracking.Tracking_ID) />
				<cfset tracking.setTracking_Code(trackingCode) />
				<cfset tracking.setOrder_ID(orderID) />
				<cfset trackingGateway.save(tracking) />
				<cfset result = true />
			<cfelse>
				<cfset result = "Error.  Tracking code duplicate." />
				<cfset result = false />
			</cfif>
		</cfcase>
		<cfcase value="deleteTracking">
			<cfset qTracking = trackingGateway.query(Tracking_Code = trackingCode) />
			<cfif qTracking.RecordCount eq 0>
				<cfset result = "Error.  Tracking code not found." />
			<cfelseif qTracking.RecordCount eq 1>
				<cfset trackingArgs = {} />
				<cfset trackingArgs.Tracking_ID = qTracking.Tracking_ID />
				<cfset trackingArgs.Deleted = 1 />
				<cfset tracking = trackingGateway.save(argumentCollection = trackingArgs) />
				<cfset result = true />
			<cfelse>
				<cfset result = "Error.  Tracking code duplicate." />
			</cfif>
		</cfcase>
	</cfswitch>
	<cfoutput>#SerializeJSON(result, false)#</cfoutput>
<cfelse>
	Bad token
</cfif>